﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Bio.Data.Providers;
using JulMar.Windows.Mvvm;
using System.Windows.Input;

/* 
* Copyright (c) 2009, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace BioBrowser.ViewModels
{
    public class AddOpenFileViewModel : ViewModel
    {
        public class OpenFileDefinitionViewModel : ViewModel
        {
            internal OpenBioDataViewModel _data;

            public string Text
            {
                get { return _data.Header; }
            }

            public string Details
            {
                get { return _data.LoadData;  }
            }

            private bool _isSelected;
            public bool IsSelected
            {
                get { return _isSelected; }
                set { _isSelected = value; OnPropertyChanged("IsSelected"); }
            }

            public bool InWorkspace { get; private set; }

            public OpenFileDefinitionViewModel(OpenBioDataViewModel vm, bool inWorkspace)
            {
                _data = vm;
                InWorkspace = inWorkspace;
            }
        }

        public class OpenFolderDefinitionViewModel : ViewModel
        {
            public string Text { get; private set; }
            public ObservableCollection<OpenFileDefinitionViewModel> Children { get; private set; }

            private bool _isExpanded;
            public bool IsExpanded
            {
                get { return _isExpanded; }
                set { _isExpanded = value; OnPropertyChanged("IsExpanded"); }
            }

            public bool Contains(OpenBioDataViewModel vm)
            {
                return Children.Where(child => child._data == vm).FirstOrDefault() != null;
            }

            public OpenFolderDefinitionViewModel(string header)
            {
                Text = header;
                IsExpanded = true;
                Children = new ObservableCollection<OpenFileDefinitionViewModel>();
            }
        }

        public ObservableCollection<OpenFolderDefinitionViewModel> Children { get; private set; }

        public ICommand SelectChildrenCommand { get; private set; }

        public AddOpenFileViewModel(IEnumerable<OpenBioDataViewModel> openFiles, IEnumerable<OpenBioDataViewModel> existingFiles)
        {
            var seqVm = new OpenFolderDefinitionViewModel("Sequence Alignments");
            var structVm = new OpenFolderDefinitionViewModel("Structure Diagrams");
            var moleculeVm = new OpenFolderDefinitionViewModel("Molecular Structure Diagrams");
            
            Children = new ObservableCollection<OpenFolderDefinitionViewModel> {seqVm, structVm, moleculeVm};
            SelectChildrenCommand = new DelegatingCommand(delegate { /* Do nothing */ }, HasSelectedItems);

            foreach (var file in openFiles)
            {
                if ((file.FormatType & (BioFormatType.Sequence | BioFormatType.AlignedSequence)) > 0)
                {
                    if (!seqVm.Contains(file))
                        seqVm.Children.Add(new OpenFileDefinitionViewModel(file, existingFiles.Contains(file)));
                }
                else if ((file.FormatType & (BioFormatType.Structure | BioFormatType.Structure2D)) > 0)
                {
                    if (!structVm.Contains(file))
                        structVm.Children.Add(new OpenFileDefinitionViewModel(file, existingFiles.Contains(file)));
                }
                else if ((file.FormatType & (BioFormatType.MoleculeStructure)) > 0)
                {
                    if (!moleculeVm.Contains(file))
                        moleculeVm.Children.Add(new OpenFileDefinitionViewModel(file, existingFiles.Contains(file)));
                }
            }
        }

        public bool HasSelectedItems()
        {
            return (Children.SelectMany(child => child.Children).FirstOrDefault(child => child.IsSelected) != null);
        }

        public IEnumerable<OpenBioDataViewModel> SelectedFiles
        {
            get
            {
                return from child in Children
                       from vm in child.Children
                       where vm.IsSelected
                       select vm._data;
            }
        }

        public void SelectionChanged(object selectedItem)
        {
            // Not a file selection?
            var currFile = selectedItem as OpenFileDefinitionViewModel;
            if (currFile == null)
                return;

            // Do not allow current workspace files to be selected
            if (currFile.InWorkspace)
                return;

            bool isSelected = currFile.IsSelected;

            // If the Ctrl key is not pressed, then deselect the world.
            if (!Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                var data = Children.SelectMany(child => child.Children);
                foreach (var child in data)
                    child.IsSelected = false;
            }

            if (!isSelected)
                currFile.IsSelected = true;
        }
    }
}
